<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Friends extends MY_Controller {

    public function __construct() {
      parent::__construct();
      
      $this->load->model('users_mod');
      $this->load->model('friends_mod');
      $this->load->model('images_mod');
    }
	/**
	 *
	 */
	public function index()
	{       
        $data['title'] = lang('friends_title');
        $data['keywords'] = lang('friends_keywords');
        $data['description'] = lang('friends_description');
        try{
          $id = $this->session->userdata('user_id');
          $data['user'] = $this->users_mod->getUserData($id,false);
          $data['main_image'] = $this->images_mod->getMainImage($id);
          $data['friends'] = $this->friends_mod->getMyFriendsList($id);
          $data['friend_requests'] = $this->friends_mod->getMyFriendRequest($id);
        } 
        catch( Exception $e )
        {
          $data['user'] = NULL;
          $data['main_image'] = NULL;
          $data['friends'] = NULL;
        }
        
        $data['menu_active'] = "profile";        
        $data['contents']  = "friends/home";   
		$this->load->view('template/main',$data);
	}
    
    /**
     *  AJAX: remove_favorite
     *  Brisanje user-a iz friend liste
     */
    public function remove_friend()
    {
        try{
          $id = $this->input->post('id'); // Image id        
                    
          if( true == $this->friends_mod->remove_friend($id) ) 
          {    
            $user_id = $this->session->userdata('user_id');
            $data['friends'] = $this->friends_mod->getMyFriendsList($user_id);
            $data['friend_requests'] = $this->friends_mod->getMyFriendRequest($user_id);
            $html = $this->load->view('friends/part/friends_list',$data,true);
           
            $my_friends_html = $this->load->view('modules/my_friends',NULL,true);
            $response = array(
                        'ind' => 1,
                        'msg' => "",
                        'num_friends' => count($data['friends']),
                        'html' => $html,
                        'my_friends_html' =>$my_friends_html
                    );
          }
          else
          {
            $response = array(
                        'ind' => 0,
                        'msg' => lang('friends_not_removed')
                    );
          }
        }
        catch (Exception $e) {
            $response = array(
                'ind' => 0,
                'msg' => 'Following errors occured: <br />' . $e->getMessage()
            );
        }
        
        echo json_encode($response);
    }
    
    /**
     *  AJAX: add friend
     *  Dodavanje user-a u friend listu
     */
    public function add_friend()
    {
        try{
          $id = $this->input->post('id'); // Image id        
                    
          if( true == $this->friends_mod->add_friend($id) ) 
          {    
            $user_id = $this->session->userdata('user_id');
            $data['friends'] = $this->friends_mod->getMyFriendsList($user_id);
            $data['friend_requests'] = $this->friends_mod->getMyFriendRequest($user_id);
            $html = $this->load->view('friends/part/friends_list',$data,true);
           
            $my_friends_html = $this->load->view('modules/my_friends',NULL,true);
            $response = array(
                        'ind' => 1,
                        'msg' => "",
                        'num_friends' => count($data['friends']),
                        'html' => $html,
                        'my_friends_html' =>$my_friends_html
                    );
          }
          else
          {
            $response = array(
                        'ind' => 0,
                        'msg' => lang('friends_not_added')
                    );
          }
        }
        catch (Exception $e) {
            $response = array(
                'ind' => 0,
                'msg' => 'Following errors occured: <br />' . $e->getMessage()
            );
        }
        
        echo json_encode($response);
    }
    
    public function send_friend_request()
    {
        try{
          $id = $this->input->post('id'); // Friend id        
          $ret = $this->friends_mod->send_friend_request($id);         
          if( 1 == $ret ) 
          {                
            $response = array(
                        'ind' => 1,
                        'msg' => lang('friend_request_sent')                        
                    );
          }
          else if( $ret == -1 )
          {
            $response = array(
                        'ind' => 0,
                        'msg' => lang('friend_request_alredy_sent')
                    );
          }
          else if( $ret == -2 ) // Nema pare
          {
            $response = array(
                        'ind' => -2,
                        'msg' => sprintf(lang('user_no_coins'),base_url()."payment.htm")
                    );
          } 
          else
          {
            $response = array(
                        'ind' => 0,
                        'msg' => lang('friend_request_not_sent')
                    );
          }
        }
        catch (Exception $e) {
            $response = array(
                'ind' => 0,
                'msg' => 'Following errors occured: <br />' . $e->getMessage()
            );
        }
        
        echo json_encode($response);
    }
    
    
    public function serchFriends()
    {
      
      try{
          $name = $this->input->post('name');        
             
          $user_id = $this->session->userdata('user_id');
          $data['friends'] = $this->friends_mod->getMyFriendsList($user_id,$name);
          $data['friend_requests'] = $this->friends_mod->getMyFriendRequest($user_id,$name);
          $html = $this->load->view('friends/part/friends_list',$data,true);

          $my_friends_html = $this->load->view('modules/my_friends',NULL,true);
          $response = array(
                      'ind' => 1,
                      'msg' => "",
                      'num_friends' => count($data['friends']),
                      'html' => $html,
                      'my_friends_html' =>$my_friends_html
                  );
          
        }
        catch (Exception $e) {
            $response = array(
                'ind' => 0,
                'msg' => 'Following errors occured: <br />' . $e->getMessage()
            );
        }
        
        echo json_encode($response);
    }
    
} // end class
/* End of file home.php */
/* Location: ./application/controllers/home.php */